package cn.nfwx.tv.helper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import cn.nfwx.tv.util.NetworkUtil;

/**
 * Created by zhouzechao on 2017/12/15.
 */

public class NetworkHelper {
    private static final String TAG = NetworkHelper.class.getSimpleName();
    private static NetworkHelper instance;
    private BroadcastReceiver connectivityBroadcastReceiver;
    private int netWorkStats = -1;
    private List<NetworkStateListener> mListener = new ArrayList<NetworkStateListener>();

    private NetworkHelper() {

    }

    public static NetworkHelper getInstance() {
        if (instance == null) {
            synchronized (NetworkHelper.class) {
                if (instance == null) {
                    instance = new NetworkHelper();
                }
            }
        }
        return instance;
    }

    public void addListener(NetworkStateListener listener) {
        if (!mListener.contains(listener)) {
            mListener.add(listener);
        }
    }

    public void removeListener(final NetworkStateListener listener) {
        if (listener == null) {
            mListener.clear();
        }
        mListener.remove(listener);
    }

    public int getConnectionType(Context context) {
        return NetworkUtil.getNetworkType(context);
    }

    public boolean hasConnection(Context context) {
        return NetworkUtil.getNetworkType(context) != NetworkUtil.NO_NETWORK;
    }

    public boolean isConnection4g(Context context) {
        return NetworkUtil.getNetworkType(context) == NetworkUtil.G_NETWORK;
    }

    public void registerConnectivityReceiver(Context context) {
        try {
            if (connectivityBroadcastReceiver == null) {
                connectivityBroadcastReceiver = new BroadcastReceiver() {
                    public void onReceive(Context context, Intent intent) {
                        String action = intent.getAction();
                        if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                            return;
                        }
                        int networkType = NetworkUtil.getNetworkType(context);
                        if (netWorkStats != networkType) {
                            netWorkStats = networkType;
                            try {
                                for (int i = 0; i < mListener.size(); i++) {
                                    NetworkStateListener listener = mListener.get(i);
                                    listener.onNetworkChange(networkType);
                                }
                            } catch (Exception e) {
                                Log.e(TAG, e.toString());
                            }
                        }
                    }
                };
            }
            IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
            context.registerReceiver(connectivityBroadcastReceiver, intentFilter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void unregisterConnectivityReceiver(Context context) {
        try {
            context.unregisterReceiver(connectivityBroadcastReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface NetworkStateListener {
        /**
         * 返回网络变化状态
         *
         * @hasDataConnection orgType  变化 chageToType  变化后
         */
        void onNetworkChange(int networkType);
    }
}
